using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;


namespace ReadCurrentProfileName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            MAPIConcubine.Fabric fabric = new MAPIConcubine.Fabric();

            // the EnumProfileNames method looks up the current available profiles for the user
            comboBox1.DataSource = fabric.EnumProfileNames();
            comboBox1.SelectedIndex = 0; 

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create an Outlook session
            object missing = System.Reflection.Missing.Value;

            // get the Outlook Application Object
            Outlook.Application outlookApplication = new Outlook.Application();

            // get the namespace object
            Outlook.NameSpace olNamespace = outlookApplication.GetNamespace("MAPI");

            // Logon to Session, here we use an already opened Outlook
            olNamespace.Logon(comboBox1.Text ,  missing, true, true);


            // Use the fabric to get the current profilename
            MAPIConcubine.Fabric fabric = new MAPIConcubine.Fabric();
            try
            {
                string currentProfileName = fabric.GetProfileName(olNamespace.MAPIOBJECT);
                MessageBox.Show(this, String.Format("The current profilename was: {0}", currentProfileName));
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this,ex.Message);
            }
            finally
            {
                fabric = null;
                olNamespace.Logoff();
                olNamespace = null;
                outlookApplication = null;

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }



            
        }

    }
}